//
//  TGGroupDetailController.m
//  GroupBuy
//
//  Created by wwj on 15/11/11.
//  Copyright © 2015年 wwj. All rights reserved.
//

#import "TGGroupDetailController.h"
#import "TGDealModel.h"
#import "TGBuyDock.h"
#import "TGRightDock.h"
#import "TGDealInfoController.h"
#import "TGDealWebController.h"
#import "TGMerchantController.h"

@interface TGGroupDetailController ()<RightDockDelegate>

@end
@implementation TGGroupDetailController

-(void)setModel:(TGDealModel *)model{
    _model = model;
}

-(void)viewDidLoad{
    [super viewDidLoad];
    //基本设置
    [self baseSetting];
    //添加顶部的购买栏
    [self addBuyDock];
    //添加右边的选项卡栏
    [self addRightDock];
}

#pragma -mark 基本设置
-(void)baseSetting{
    self.view.backgroundColor = kBgColor;
    self.title = _model.title;
    
    [self.navigationController.view addGestureRecognizer:[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(drag:)]];
}

- (void)drag:(UIPanGestureRecognizer *)pan{
    CGFloat tx = [pan translationInView:pan.view].x;
    
    if (pan.state == UIGestureRecognizerStateEnded) {
        CGFloat halfW = pan.view.frame.size.width * 0.5;
        if (tx >= halfW) { // 已经往右边挪动超过一半了
            if (_hiddenBlock) {
                _hiddenBlock();
            }
        } else {
            [UIView animateWithDuration:0.3 animations:^{
                pan.view.transform = CGAffineTransformIdentity;
            }];
        }
    } else { // 移动控制器的view
        if (tx < 0) { // 向左边拽
            tx *= 0.3;
        }
        pan.view.transform = CGAffineTransformMakeTranslation(tx, 0);
    }
}

#pragma -mark 添加顶部的购买栏
- (void)addBuyDock{
    TGBuyDock *buyDock = [TGBuyDock buyDock];
    buyDock.model = _model;
    buyDock.frame = CGRectMake(0, 0, self.view.frame.size.width, 0);
    [self.view addSubview:buyDock];
}

#pragma -mark 添加右边的选项卡栏
- (void)addRightDock{
    TGRightDock *rightDock = [TGRightDock rightDock];
    CGSize size = rightDock.frame.size;
    rightDock.frame = CGRectMake(self.view.frame.size.width - size.width, self.view.frame.size.height-size.height-100, 0, 0);
    rightDock.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin;
    rightDock.delegate = self;
    [self.view addSubview:rightDock];
    
    //初始化子控制器
    TGDealInfoController *infoVC = [[TGDealInfoController alloc] init];
    infoVC.model = _model;
    [self addChildVCWith:infoVC];
    
    TGDealWebController *webVC = [[TGDealWebController alloc] init];
    webVC.model =_model;
    [self addChildVCWith:webVC];
    
    TGMerchantController *merchantVC = [[TGMerchantController alloc] init];
    merchantVC.model = _model;
    [self addChildVCWith:merchantVC];
    
    //默认选择第一个
    [self didSelectItemWithDock:rightDock from:0 to:0];

}

-(void)addChildVCWith:(UIViewController *)vc{
    [self addChildViewController:vc];
}

-(void)didSelectItemWithDock:(TGRightDock *)rightDock from:(int)from to:(int)to{
    UIViewController *oldVC = self.childViewControllers[from];
    [oldVC.view removeFromSuperview];
    
    UIViewController *newVC = self.childViewControllers[to];
    newVC.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    newVC.view.frame = CGRectMake(0, 0, self.view.frame.size.width - rightDock.frame.size.width, self.view.frame.size.height);
    
    [self.view insertSubview:newVC.view atIndex:0];
}
@end
